home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / speller2.zip / SPELL.HPP < prev    next >
C/C++ Source or Header  |  1995-02-06  |  14KB  |  519 lines

  1. #if !defined(SPELL_H)
  2. #define SPELL_H
  3.  
  4. #if !defined(__SPOBJS_H)
  5. #include "spobjs.h"
  6. #endif
  7.  
  8. #define ItemsInPage 2048
  9.  
  10. class BaseDic {
  11. public:
  12. unsigned IndexCacheSize;
  13. unsigned DictCacheSize;
  14. tagHeader header;
  15. int curLetter;
  16. long curWord; //
  17. int error;
  18. static const char*sfx[NSFX]; 
  19.  
  20. };
  21.  
  22.  
  23. class MainDictionary :virtual public BaseDic{
  24.  
  25. public:
  26.  
  27.         MainDictionary(char *name ,unsigned Dcachesize =16384,
  28.                 unsigned IcacheSize =16384);
  29.  
  30.         virtual int lookup(char *word) ;
  31.         virtual void reset() =0;      //
  32.  
  33.     protected:
  34.     char *word;
  35.        virtual int load_mdict(char*name) =0;
  36.  
  37.        virtual int load_index(char* name) =0;
  38.  
  39.        virtual long search(char *,long) =0;
  40.  
  41.         void * index ;     // index array
  42.         void * dict ;     //
  43.  
  44.         long *pages;
  45.         long cpages;
  46.  
  47.  
  48.         long  from ,to;
  49.                 long blkcnt; //
  50.                 unsigned int offset; //
  51.         //static const char *sfx[31];  ////// new !!
  52.                 //static const char *hiValue;
  53.  
  54. };
  55.  
  56. ////////////////////////////////////////////////////////////////////
  57. ////////////////////////////////////////////////////////////////////
  58.  
  59. #if !defined (__386__) && !defined ( DOS386 ) && !defined (__DPMI16__)
  60.  
  61. class XMSDictionary :public MainDictionary {
  62. public:
  63.  
  64.         XMSDictionary(char *name ,unsigned Dcachesize =16384,
  65.                 unsigned IcacheSize =16384);
  66.         ~XMSDictionary();
  67.  
  68.         virtual char *nextWord() ;
  69.         virtual void reset();      //
  70.  
  71.     protected:
  72.  
  73.     virtual long search(char* ,long);
  74.         virtual int load_mdict(char*name) ;
  75.  
  76.        virtual int load_index(char* name) ;
  77.        int cmpread(FILE *);
  78.                 void reset_fifo();
  79.         void reset_block();
  80.  
  81. };
  82.  
  83. inline void XMSDictionary::reset()
  84. {
  85. blkcnt=0;
  86. curWord=0;
  87. char *tmp=(char *)((xms*)dict)->at(0);
  88. curLetter=0;
  89. offset=0;
  90.  
  91. }
  92.  
  93. inline void XMSDictionary::reset_block()
  94. {
  95. ((xms*)dict)->curblock=-1;
  96. ((xms*)index)->curblock=-1;
  97.  
  98. }
  99.  
  100. inline void XMSDictionary::reset_fifo()
  101. {
  102. ((xms*)dict)->fifo_reset();
  103. ((xms*)index)->fifo_reset();
  104. }
  105.  
  106. ////////////////////////////////////////////////////////////////////
  107. ////////////////////////////////////////////////////////////////////
  108.  
  109.  
  110. #if defined (__BORLANDC__)
  111. class EMSDictionary :public MainDictionary {
  112. public:
  113.  
  114.         EMSDictionary(char *name ,unsigned Dcachesize =16384,
  115.                 unsigned IcacheSize =16384);
  116.         ~EMSDictionary();
  117.  
  118.         virtual char *nextWord() ;
  119.         virtual void reset();      //
  120.  
  121.     protected:
  122.  
  123.     virtual long search(char* ,long);
  124.         virtual int load_mdict(char*name) ;
  125.  
  126.        virtual int load_index(char* name) ;
  127.        int cmpread(FILE *);
  128.                 void reset_fifo(){;}
  129.         void reset_block();
  130.  
  131. };
  132.  
  133. inline void EMSDictionary::reset()
  134. {
  135. blkcnt=0;
  136. curWord=0;
  137. char *tmp=(char *)((ems*)dict)->at(0);
  138. curLetter=0;
  139. offset=0;
  140.  
  141. }
  142.  
  143. inline void EMSDictionary::reset_block()
  144. {
  145. ((ems*)dict)->curblock=-1;
  146. ((ems*)index)->curblock=-1;
  147.  
  148. }
  149. #endif
  150.  
  151. ////////////////////////////////////////////////////////////////////
  152. ////////////////////////////////////////////////////////////////////
  153.  
  154.  
  155. class CXMSDictionary :public MainDictionary {
  156. public:
  157.  
  158.         CXMSDictionary(char *name ,unsigned Dcachesize =16384,
  159.                 unsigned IcacheSize =16384);
  160.         ~CXMSDictionary(); // !!!!!!!!!!!!
  161.  
  162.         virtual char *nextWord() {return nextword();}
  163.         virtual void reset();      //
  164.     virtual int lookup(char *);
  165.     protected:
  166.  
  167.         virtual long search(char* ,long){return 1;}
  168.     virtual int load_mdict(char*name) ;
  169.  
  170.        virtual int load_index(char* name) {return 1;}
  171.  
  172.                 //void reset_fifo(){;}
  173.         char *nextword();
  174.  
  175.                 tagCheader cheader;
  176.                 tagAddHeader add_header;
  177.         int pblock[27]; //ptr to block
  178.         int offs[27];
  179.         IndexNode *idx;
  180.         char *prevword;
  181.         char *tmp;
  182.                 void reset_block();
  183.                 void reset_fifo();
  184.  
  185.  
  186.  
  187. };
  188.  
  189. inline void CXMSDictionary::reset()
  190. {
  191. blkcnt=0;
  192. curWord=0;
  193. prevword="";
  194. curLetter=0;
  195. offset=0;
  196. reset_block();
  197. reset_fifo();
  198. }
  199.  
  200. inline void CXMSDictionary::reset_block()
  201. {
  202.   ((xms*)dict)->curblock=-1;
  203.    tmp=(char*)((xms*)dict)->ats(cheader.firstLetter[curLetter]);
  204.  
  205. }
  206.  
  207. inline void CXMSDictionary::reset_fifo()
  208. {
  209. ((xms*)dict)->fifo_reset();
  210. }
  211.  
  212.  
  213. #endif
  214.  
  215. ////////////////////////////////////////////////////////////////////
  216. ////////////////////////////////////////////////////////////////////
  217.  
  218. class DBDictionary :public MainDictionary {
  219. public:
  220.  
  221.         DBDictionary(char *name ,unsigned Dcachesize =16384,
  222.                 unsigned IcacheSize =16384);
  223.         ~DBDictionary();
  224.  
  225.         virtual char *nextWord() {return nextword();}
  226.         virtual void reset();      //
  227.     virtual int lookup(char *);
  228.     protected:
  229.  
  230.         virtual long search(char* ,long){return 1;}
  231.     virtual int load_mdict(char*name) ;
  232.  
  233.        virtual int load_index(char* name) {return 1;}
  234.  
  235.         void reset_fifo(){;}
  236.         void reset_block(){
  237.           fseek((FILE *)dict,cheader.firstLetter[curLetter],SEEK_SET);}
  238.         void CreateIndex();
  239.         char *nextword();
  240.                 tagCheader cheader;
  241.                 tagAddHeader add_header;
  242.  
  243.         int pblock[27]; //ptr to block
  244.         int offs[27];
  245.         IndexNode *idx;
  246.         char *prevword;
  247.  
  248.  
  249.  
  250. };
  251.  
  252. inline void DBDictionary::reset()
  253. {
  254. blkcnt=0;
  255. curWord=0;
  256. //char *tmp="";
  257. prevword="";
  258. curLetter=0;
  259. offset=0;
  260. reset_block();
  261.  
  262. }
  263.  
  264.  
  265. ////////////////////////////////////////////////////////////////////
  266. //                                                                //
  267. //                                                                //
  268. //                                                                //
  269. //                                                                //
  270. ////////////////////////////////////////////////////////////////////
  271.  
  272.  
  273. class spell:virtual public BaseDic{
  274. public:
  275.  
  276.         static spell *Spell;   //<---- static member points to this
  277.         MatchTable *suggestion;
  278.         FastDictionary *userDic;
  279.         FastDictionary *tempDic;
  280.         FastDictionary *ignoreDic;
  281. //    Table * fastDic;
  282.  
  283.  
  284.         spell(char *main_dic="main.dic",char *user_dic="user.dic",
  285.       char *temp_dic="temp.dic") ;
  286.         ~spell(void);
  287. //
  288. //   Main public interface
  289. //
  290.         int check(char *);
  291.         int suggest(const char *);
  292.     int suggest2(const char *);
  293.         char *ignore(char *word){return ignoreDic->insert(word);}
  294.         int add(char *word ,FastDictionary *dic);
  295. //
  296.     int rcheck(char *);
  297.     int addToUserDic(char *word) {return add(word,userDic);}
  298.         int addToTempDic(char *word) {return add(word,tempDic);}
  299.     void saveUserDic(){ userDic->save(userDicName);}
  300.     void saveTempDic(){ tempDic->save(tempDicName);}
  301.         int merge(FastDictionary *dic,char *name="main.dic");
  302.         //int reload(MainDictionary *,char *name="main.dic");
  303.         int reload(FastDictionary *,char *name="user.dic");
  304.         void SetLevel(int Level) { level |= Level;}
  305.         int getLastError(){return error;}
  306.         void clearError(){ error = 0;}
  307.         int  writeWord(char *s,FILE *,FILE *);
  308.         virtual int Lookup(char *word)=0;
  309.         virtual char *NextWord()=0;
  310.         int isModified();
  311. protected:
  312.     virtual void reset_block()=0;
  313.     virtual void reset_fifo()=0;
  314.         int MaxMatches ;
  315.         int like;
  316.         int level; // level for suggesstion of misspelled words
  317.         int found;
  318.         int minwordlen;
  319.         int maxwordlen;
  320.  
  321.         int error;
  322.         char *tempword;
  323.         char *mainDicName;
  324.         char *userDicName;
  325.         char *tempDicName;
  326.         //////////////////
  327.     long offset;
  328.         int curoffs;
  329.         int UpperFlag;
  330.         int cLetter;
  331.     long lineCount;
  332.         tagHeader newheader;
  333.         tagAddHeader newaheader;
  334.         tagCheader newcheader;
  335.         IndexNode *newIndex;
  336.         char *prevw;
  337.         char *tempw;
  338.     char *wrd;
  339.         long *new_pages;
  340.         long new_cpage;
  341.         long npages;
  342.         long page_pos;
  343.         long index_pos;
  344.  
  345. };
  346.  
  347. inline spell::isModified()
  348. {
  349.   int val=0;
  350.  if(userDic->modified)
  351.         val |=USER_DIC;
  352.  if(tempDic->modified)
  353.         val |=TEMP_DIC;
  354.  return val;
  355. }
  356. ////////////////////////////////////////////////////////////////////
  357. ////////////////////////////////////////////////////////////////////
  358.  
  359. #if !defined (__386__) && !defined ( DOS386 ) && !defined (__DPMI16__)
  360.  
  361. class XmsSpell : public spell,public  XMSDictionary{
  362. public:
  363.         XmsSpell(char *main_dic="main.dic",char *user_dic="user.dic",
  364.       char *temp_dic="temp.dic",unsigned int Dcache=16384,
  365.       unsigned int Icache=16384)
  366.         : spell(main_dic,user_dic,temp_dic) ,
  367.         XMSDictionary(main_dic ,Dcache,Icache){
  368.             maxwordlen = max(header.maxSize,userDic->maxWordLen);
  369.               error=XMSDictionary::error;}
  370.  
  371.         int reload(XMSDictionary *dic,char *name="main.dic")
  372.                 {
  373.                 unsigned ICache=dic->IndexCacheSize;
  374.                 unsigned DCache=dic->DictCacheSize;
  375.  
  376.                 delete  dic;
  377.                 clearError();
  378.                 dic = new XMSDictionary(name, DCache, ICache);
  379.                 if(dic ->error)
  380.                 {
  381.                         error++;
  382.                         return 0;
  383.                         }
  384.                 return 1;
  385.                 };
  386.  
  387.         inline virtual int Lookup(char *word){
  388.                         return XMSDictionary::lookup(word);}
  389.         inline virtual char *NextWord() {
  390.                         return XMSDictionary::nextWord();}
  391.     protected:
  392.     inline void reset_block(){XMSDictionary::reset_block();}
  393.     inline void reset_fifo(){ XMSDictionary::reset_fifo();}
  394.  
  395. };
  396.  
  397. ////////////////////////////////////////////////////////////////////
  398. ////////////////////////////////////////////////////////////////////
  399.  
  400. class CXmsSpell : public spell,public  CXMSDictionary{
  401. public:
  402.         CXmsSpell(char *main_dic="main.dic",char *user_dic="user.dic",
  403.       char *temp_dic="temp.dic",unsigned int Dcache=16384,
  404.       unsigned int Icache=16384)
  405.         : spell(main_dic,user_dic,temp_dic) ,
  406.         CXMSDictionary(main_dic ,Dcache,Icache){
  407.             maxwordlen = max(header.maxSize,userDic->maxWordLen);
  408.               error=CXMSDictionary::error;}
  409.  
  410.         int reload(CXMSDictionary *dic,char *name="main.dic")
  411.                 {
  412.                 unsigned ICache=dic->IndexCacheSize;
  413.                 unsigned DCache=dic->DictCacheSize;
  414.  
  415.                 delete dic;
  416.                 clearError();
  417.                 dic = new CXMSDictionary(name, DCache, ICache);
  418.                 if(dic ->error)
  419.                         {
  420.                         error++;
  421.                         return 0;
  422.                 }
  423.                 return 1;
  424.                 }
  425.  
  426.         inline virtual int Lookup(char *word){
  427.                         return lookup(word);}
  428.         inline virtual char *NextWord() {
  429.                         return nextWord();}
  430.     protected:
  431.         inline void reset_block(){CXMSDictionary::reset_block();}
  432.         inline void reset_fifo(){ CXMSDictionary::reset_fifo();}
  433.  
  434. };
  435.  
  436. ////////////////////////////////////////////////////////////////////
  437. ////////////////////////////////////////////////////////////////////
  438.  
  439. #if defined (__BORLANDC__)
  440. class EmsSpell : public spell,public  EMSDictionary{
  441. public:
  442.         EmsSpell(char *main_dic="main.dic",char *user_dic="user.dic",
  443.       char *temp_dic="temp.dic",unsigned int Dcache=16384,
  444.       unsigned int Icache=16384)
  445.         : spell(main_dic,user_dic,temp_dic) ,
  446.         EMSDictionary(main_dic ,Dcache,Icache){
  447.             maxwordlen = max(header.maxSize,userDic->maxWordLen);
  448.               error=EMSDictionary::error;}
  449.  
  450.         int reload(EMSDictionary *dic,char *name="main.dic")
  451.                 {
  452.                 unsigned ICache=dic->IndexCacheSize;
  453.                 unsigned DCache=dic->DictCacheSize;
  454.  
  455.                 delete dic;
  456.                 clearError();
  457.                 dic = new EMSDictionary(name, DCache, ICache);
  458.                 if(dic ->error)
  459.                         {
  460.                         error++;
  461.                         return 0;
  462.                 }
  463.                 return 1;
  464.                 }
  465.  
  466.         inline virtual int Lookup(char *word){
  467.                         return EMSDictionary::lookup(word);}
  468.         inline virtual char *NextWord() {
  469.                         return EMSDictionary::nextWord();}
  470.     protected:
  471.         inline void reset_block(){EMSDictionary::reset_block();}
  472.         inline void reset_fifo(){ EMSDictionary::reset_fifo();}
  473.  
  474. };
  475.  
  476. #endif
  477.  
  478. #endif // !protmode
  479. ////////////////////////////////////////////////////////////////////
  480. ////////////////////////////////////////////////////////////////////
  481.  
  482. class DiskSpell : public spell,public  DBDictionary{
  483. public:
  484.     DiskSpell(char *main_dic="main.dic",char *user_dic="user.dic",
  485.       char *temp_dic="temp.dic",unsigned int Dcache=16384,
  486.       unsigned int Icache=16384)
  487.     : spell(main_dic,user_dic,temp_dic) ,
  488.         DBDictionary(main_dic ,Dcache,Icache){
  489.             maxwordlen = max(header.maxSize,userDic->maxWordLen);
  490.               error=DBDictionary::error;}
  491.  
  492.         int reload(DBDictionary *dic,char *name="main.dic")
  493.                 {
  494.                 unsigned ICache=dic->IndexCacheSize;
  495.                 unsigned DCache=dic->DictCacheSize;
  496.  
  497.                 delete dic;
  498.                 clearError();
  499.                 dic = new DBDictionary(name, DCache, ICache);
  500.                 if(dic ->error)
  501.                         {
  502.                         error++;
  503.                         return 0;
  504.                 }
  505.                 return 1;
  506.                 }
  507.  
  508.         inline virtual int Lookup(char *word){
  509.                         return DBDictionary::lookup(word);}
  510.         inline virtual char *NextWord() {
  511.                         return DBDictionary::nextWord();}
  512.     protected:
  513.     inline void reset_block(){DBDictionary::reset_block();}
  514.     inline void reset_fifo(){ DBDictionary::reset_fifo();}
  515.  
  516. };
  517. #endif // SPELL_H
  518.  
  519.